# -*- coding: UTF-8 -*-

"""
Rabbitmq
"""

import socket
import fcntl
import struct
import tornado.ioloop

from tornado.options    import options
from loog.mylog          import flogger as logger

from stormed            import Connection as StormedConnection
from .consumers         import consumer
from .models            import SyncData
from configure          import mq_conf


class Rabbitmq(object):
    """ Rabbitmq """

    def __init__(self):

        self.conn = StormedConnection(
            host      = mq_conf.MQ_HOST,
            username  = mq_conf.MQ_USERNAME,
            password  = mq_conf.MQ_PASSWORD,
            heartbeat = mq_conf.MQ_HEARTBEAT
        )

        self.conn.connect(self._on_amqp_connection_)
        self.conn.on_disconnect = self._on_amqp_disconnect_


    def add_consume(self, queue):
        self.ch.consume(queue, consumer.consume, no_ack=True)

    def _on_amqp_connection_(self):
        self.ch = self.conn.channel()
        options['ch'] = self.ch

        """
        self.ch.exchange_declare(
                                exchange = mq_conf.Sync_Send_Data_Exchange,
                                type="direct",
                                durable = True)

        self.ch.exchange_declare(
                                exchange = mq_conf.Image_Resize_Exchange,
                                type="direct",
                                durable = True)

        self.ch.queue_declare(
                                queue = mq_conf.Image_Resize_Queue,
                                durable=True)

        self.ch.queue_declare(
                                queue = mq_conf.Image_Resize_Call_Back_Queue,
                                durable=False,
                                auto_delete=True)

        self.ch.queue_bind(
                                queue=mq_conf.Image_Resize_Queue,
                                exchange=mq_conf.Image_Resize_Exchange,
                                routing_key=mq_conf.Image_Resize_Queue)

        self.ch.queue_bind(
                                queue=mq_conf.Image_Resize_Call_Back_Queue,
                                exchange=mq_conf.Image_Resize_Exchange,
                                routing_key=mq_conf.Image_Resize_Call_Back_Queue)
        """

        SyncData.configure(self.ch)

        self._consume_queues_()


    def _consume_queues_(self):
        """
        consume_queues = [
            mq_conf.Image_Resize_Call_Back_Queue,
        ]
        for consume_queue in consume_queues:
            self.ch.consume(consume_queue, consumer.consume, no_ack=True)
        """
        pass

    def _on_amqp_disconnect_(self):
        logger.info("[*]on_amqp_disconnect")
        logger.info("[*]will close tornado")
        try:
            tornado.ioloop.IOLoop.instance().stop()
        except AttributeError, e:
            logger.info(e.message)
        finally:
            exit(1)

    def _get_ip_address(self, ifname):
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        return socket.inet_ntoa(fcntl.ioctl(
            s.fileno(),
            0x8915,
            struct.pack('256s', ifname[:15])
        )[20:24])

